package com.flymobi.service;

import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.fw.modules.SM;
import com.flymobi.po.SmsTask;
import com.flymobi.po.Sp;
import com.flymobi.vo.SmsTongjiSearchVo;

public class TongjiService extends CommonService {
	
	public Long getFilterCount(SmsTongjiSearchVo param) {
		String hql="from SmsTask m where 1=1 and m.shenhe!=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				hql+=" and m.managerId=?";
				paramList.add(param.getSpId());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		List<SmsTask> taskList=dao.find(SmsTask.class,hql,paramList,0,1000000000);
		long count=0;
		for(SmsTask task:taskList){
			count+=task.getMobileCount();
		}
		return count;
	}
	
	public Long getSendCount(SmsTongjiSearchVo param) {
		
		String hql="select count(*) from SmsLog m where 1=1";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				Sp sp=SM.SpService.get(param.getSpId()+"");
				hql+=" and m.spNumber like ?";
				paramList.add(sp.getSpNumber()+sp.getSubSpNumber()+"%");
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		
		return dao.getCount(hql, paramList);
	}
	
	public Long getSendSuccessCount(SmsTongjiSearchVo param) {
		
		String hql="select count(*) from SmsLog m where 1=1 and m.status=1";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				Sp sp=SM.SpService.get(param.getSpId()+"");
				hql+=" and m.spNumber like ?";
				paramList.add(sp.getSpNumber()+sp.getSubSpNumber()+"%");
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		
		return dao.getCount(hql, paramList);
	}
	
	public Long getWaitSendCount(SmsTongjiSearchVo param) {
		
		String hql="select count(*) from SmsLog m where 1=1 and m.status=0";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				Sp sp=SM.SpService.get(param.getSpId()+"");
				hql+=" and m.spNumber like ?";
				paramList.add(sp.getSpNumber()+sp.getSubSpNumber()+"%");
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		
		return dao.getCount(hql, paramList);
	}
	
	public Long getSendReportSuccessCount(SmsTongjiSearchVo param) {
		
		String hql="select count(*) from SmsLog m where 1=1 and m.status=1 and m.errorCode=0";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				Sp sp=SM.SpService.get(param.getSpId()+"");
				hql+=" and m.spNumber like ?";
				paramList.add(sp.getSpNumber()+sp.getSubSpNumber()+"%");
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		
		return dao.getCount(hql, paramList);
	}
	
	public Long getSendReportFailCount(SmsTongjiSearchVo param) {
		
		String hql="select count(*) from SmsLog m where 1=1 and m.status=1 and m.errorCode>0";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				Sp sp=SM.SpService.get(param.getSpId()+"");
				hql+=" and m.spNumber like ?";
				paramList.add(sp.getSpNumber()+sp.getSubSpNumber()+"%");
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		
		return dao.getCount(hql, paramList);
	}
	
	public Long getSendNoReport(SmsTongjiSearchVo param) {
		
		String hql="select count(*) from SmsLog m where 1=1 and m.status=1 and m.errorCode<0";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(param.getSpId()!=null){
				Sp sp=SM.SpService.get(param.getSpId()+"");
				hql+=" and m.spNumber like ?";
				paramList.add(sp.getSpNumber()+sp.getSubSpNumber()+"%");
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		
		return dao.getCount(hql, paramList);
	}
	
}
